/*
 * Filename:    CognosReportController.java
 * Copyright:   2015 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2015-3-4
 */
package com.paic.mhis.hcpms.common.sso.web.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.HttpProcessUtil;
import com.paic.mhis.hcpms.common.sso.biz.service.CognosReportService;
import com.paic.mhis.hcpms.common.sso.dto.CognosReportDTO;
import com.paic.mhis.hcpms.common.sso.dto.CognosRoleDTO;
import com.paic.mhis.hcpms.common.sso.web.vo.CognosReportVO;
import com.paic.pafa.appclient.annotation.BizServicesProxy;
import com.paic.pafa.exceptions.BusinessException;

@Controller
@RequestMapping("/cognosReportController/")
public class CognosReportController {
	@BizServicesProxy(beanName = "cognosReportServiceImpl")
	private CognosReportService cognosReportService;

	/**
	 * 加载对应的数据报表信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("loadCognosReport.do")
	@ResponseBody
	public Map<String, Object> loadCognosReport(HttpServletRequest request, HttpServletResponse response) {

		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		String reportName = request.getParameter("reportName");
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");

		int page = Integer.parseInt(request.getParameter("page"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		int startNum = (page - 1) * pageSize;
		int endNum = startNum + pageSize;

		Map<String, Object> parameterMap = new HashMap<String, Object>();
		if (StringUtils.hasText(startDate)) {

			parameterMap.put("createDateStart", startDate);
		}
		if (StringUtils.hasText(endDate)) {
			parameterMap.put("createDateEnd", endDate);
		}

		Map<String, Object> returnMap = this.cognosReportService.loadCognosReportInfo(user, reportName, startNum,
				endNum, parameterMap);
		return returnMap;
	}

	/**
	 * 报表信息展示
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws BusinessException
	 */
	@RequestMapping("cognosReportAuthorizes.do")
	@ResponseBody
	public Map<String, Object> cognosReportAuthorizes(HttpServletRequest request, HttpServletResponse response)
			throws BusinessException {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		Map<String, Object> returnMap = new HashMap<String, Object>();
		List<CognosReportDTO> cognosReports = cognosReportService.findCognosReports(paramMap);
		returnMap.put("cognosReports", cognosReports);
		return returnMap;
	}

	/**
	 * 新增角色与报表关系
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws BusinessException
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 */
	@RequestMapping(value = "saveCognosReportAuthorize.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> saveCognosReportAuthorize(HttpServletRequest request, HttpServletResponse response)
			throws BusinessException, UnsupportedEncodingException, IOException {
		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		String str = HttpProcessUtil.readRequestBody(request);
		JSONObject transferJson = JSON.parseObject(str);
		if (transferJson == null) {
			return null;
		}
		String roleIds = transferJson.get("roleIds").toString();
		String reportUrlIds = transferJson.get("reportUrlIds").toString();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		if (StringUtils.hasText(roleIds)) {
			String[] paramStr = roleIds.split("\\;\\s*|;\\s*|,\\s*|，\\s*");
			paramMap.put("roleIds", paramStr);
		}
		if (StringUtils.hasText(reportUrlIds)) {
			String[] paramStr = reportUrlIds.split("\\;\\s*|;\\s*|,\\s*|，\\s*");
			paramMap.put("reportUrlIds", paramStr);
		}
		boolean istrue = cognosReportService.saveCognosReportAuthorize(transferJson, user.getUsername(), paramMap);
		Map<String, Object> returnMap = new HashMap<String, Object>();
		returnMap.put("istrue", istrue);
		return returnMap;
	}

	@RequestMapping("processCognosReportResource.do")
	@ResponseBody
	public Map<String, Object> saveCognosReportResource(HttpServletRequest request, HttpServletResponse response) {
		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		String reportName = request.getParameter("reportName");
		String reportUrl = request.getParameter("reportUrl");
		String processType = request.getParameter("processType");
		String cognosReportId = request.getParameter("cognosReportId");

		CognosReportDTO bean = new CognosReportDTO();

		bean.setReportName(reportName);
		bean.setReportUrl(reportUrl);
		bean.setCreateUser(user.getUsername());
		bean.setUpdateUser(user.getUsername());

		if (StringUtils.hasText(cognosReportId)) { // 更新或者删除
			bean.setId(cognosReportId);
		}
		Map<String, Object> returnMap = this.cognosReportService.processCognosReportInfo(user, processType, bean);
		return returnMap;

	}

	/**
	 * 查询角色与报表信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws BusinessException
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 */
	@RequestMapping("cognosReportQueryRole.do")
	@ResponseBody
	public Map<String, Object> cognosReportQueryRole(@ModelAttribute CognosReportVO search) throws BusinessException,
			UnsupportedEncodingException, IOException {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		if (StringUtils.hasText(search.getRoleIds())) {
			String[] paramStr = search.getRoleIds().split("\\；\\s*|;\\s*|,\\s*|，\\s*");
			paramMap.put("roleIds", paramStr);
		} else {
			paramMap.put("roleIds", null);
		}
		Map<String, Object> returnMap = new HashMap<String, Object>();
		List<CognosRoleDTO> cognosReportQueryRoles = cognosReportService.cognosReportQueryRole(paramMap);
		returnMap.put("cognosReportQueryRoles", cognosReportQueryRoles);
		return returnMap;
	}

	/**
	 * 展示当前已授权报表信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws BusinessException
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 */
	@RequestMapping("cognosReportAuthorizeQuery.do")
	@ResponseBody
	public Map<String, Object> cognosReportAuthorizeQuery(@ModelAttribute CognosReportVO search)
			throws BusinessException, UnsupportedEncodingException, IOException {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		if (StringUtils.hasText(search.getRoleIds())) {
			String[] paramStr = search.getRoleIds().split("\\；\\s*|;\\s*|,\\s*|，\\s*");
			paramMap.put("roleIds", paramStr);
		} else {
			paramMap.put("roleIds", null);
		}
		Map<String, Object> returnMap = new HashMap<String, Object>();
		List<CognosReportDTO> authorizeQuerys = cognosReportService.cognosReportAuthorizeQuery(paramMap);
		returnMap.put("authorizeQuerys", authorizeQuerys);
		return returnMap;
	}

	@RequestMapping("loadSigleCognosReport.do")
	@ResponseBody
	public Map<String, Object> loadSigleCognosReport(HttpServletRequest request, HttpServletResponse response) {

		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		String reportName = request.getParameter("reportName");
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");

		int page = Integer.parseInt(request.getParameter("page"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		int startNum = (page - 1) * pageSize;
		int endNum = startNum + pageSize;

		Map<String, Object> parameterMap = new HashMap<String, Object>();
		if (StringUtils.hasText(startDate)) {

			parameterMap.put("createDateStart", startDate);
		}
		if (StringUtils.hasText(endDate)) {
			parameterMap.put("createDateEnd", endDate);
		}

		Map<String, Object> returnMap = this.cognosReportService.loadSignleCognosReportInfo(user, reportName, startNum,
				endNum, parameterMap);
		return returnMap;
	}

	@RequestMapping("deletCognosReportInfo.do")
	@ResponseBody
	public Map<String, Object> deletCognosReportInfo(HttpServletRequest request, HttpServletResponse response) {

		String[] reportIds = request.getParameterValues("reportIds");

		this.cognosReportService.batchDeleteCognosReportInfo(reportIds);
		Map<String, Object> returnMap = new HashMap<String, Object>();

		returnMap.put("flag", true);
		returnMap.put("msg", "success");
		return returnMap;
	}

	/**
	 * 授权
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping("authorizeCognosReportInfo.do")
	@ResponseBody
	public Map<String, Object> authorizeCognosReportInfo(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, IOException {
		UserDetails user = (UserDetails) request.getSession().getAttribute("user");

		String[] reportIds = request.getParameterValues("reportIds");
		String[] roleIds = request.getParameterValues("roleIds");

		this.cognosReportService.saveCognosReportAuthorizeInfo(roleIds, reportIds, user);

		Map<String, Object> returnMap = new HashMap<String, Object>();
		returnMap.put("flag", true);
		returnMap.put("msg", "success");
		return returnMap;
	}

	/**
	 * 加载已经授权的角色
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("loadReportRoleInfo.do")
	@ResponseBody
	public Map<String, Object> loadReportRoleInfo(HttpServletRequest request, HttpServletResponse response) {

		List<CognosRoleDTO> list = this.cognosReportService.loadRoleInfo();
		Map<String, Object> returnMap = new HashMap<String, Object>();
		returnMap.put("flag", true);
		returnMap.put("list", list);
		return returnMap;
	}

	/**
	 * 根据roleID 加载对应的报表信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("loadReportInfoByRole.do")
	@ResponseBody
	public Map<String, Object> loadReportInfoByRole(HttpServletRequest request, HttpServletResponse response) {

		String roleId = request.getParameter("roleId");
		List<CognosReportDTO> list = this.cognosReportService.loadReportByRoleId(roleId);
		Map<String, Object> returnMap = new HashMap<String, Object>();
		returnMap.put("flag", true);
		returnMap.put("list", list);
		return returnMap;
	}

}
